//引入express
let express = require('express');

//引入ejs
let ejs = require('ejs');

//引入http和https
let http = require('http');
let https = require('https');

//引入fs
let fs = require('fs');

//引入path
let path = require('path');

//创建应用
let app = express();

//修改扩展名
app.engine('.html',ejs.__express);

//静态化
app.use('/static/', express.static(path.join(process.cwd(), './static/')));
//静态化logo
app.use('/favicon.ico', express.static(path.join(process.cwd(), './static/favicon.ico')))


//接口
app.get('/', (req,res) => {
    //渲染页面
    res.render('home.html')
})

//mock数据
app.use('/data/',(req, res, next) => {
    //添加扩展名
    // req.url += '.json'
    // console.log(req.url),
    //切割路径
    let arr = req.url.split('?');
    //添加扩展名
    arr[0] += '.json';
    //拼接字符串
    req.url = arr.join('?');
    //必须执行next
    next();
}, express.static(path.join(process.cwd(), './static/data/')))

//启动端口号
// app.listen(3000)

//端口号
let httpPort = 3000;
let httpsPort = 3001;

//引入秘钥
let key = fs.readFileSync(path.join(process.cwd(), './ssl/private.pem'));
let cert = fs.readFileSync(path.join(process.cwd(), './ssl/file.crt'));

//启动端口号      HTTP
http.createServer(app)
    //监听端口号
    .listen(httpPort, () => console.log('http port listen at' + httpPort));

//启动端口号     HTTPS
https.createServer({key, cert }, app)
    //监听端口号
    .listen(httpsPort, () => console.log('https port listen at' + httpsPort));